<?php
/*
 * 车型库汽车批发类
 * @package    auto
 * @author     Wl
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: 6a03183b04073c80f6d26285d585a649ed00b5aa $
 */

defined('IN_IXDCW') or exit('Access Denied');

class AutoSaleModule extends BasicModule {

	/** @var 条目ID */
	private $_itemid;

	/** @var string 表格 */
	private $_table;

	/** @var  数据库前缀 */
	private $_pre;


	function __construct() {
		parent::__construct();
		$this->_pre = $this->_db->getPre();
		$this->_table = Table::name(Table::NAME_AUTO_SALE);
		$this->_fields     = array('arbid','arcid','amid','catid','mycatid','areaid','typeid','level','title','tag','style','fee','introduce','model','standard','brand','unit','price','minamount','amount','days','thumb','thumb1','thumb2','tag','status','hits','username','totime','editor','addtime','adddate','edittime',
			'editdate','ip','template','linkurl','filepath','elite','note','company','truename','telephone','mobile','address','email','msn','qq','ali','skype');
    }

	public function setItemid($itemid) {
		$this->_itemid = $itemid;
	}

	public function getItemid() {
		return $this->_itemid;
	}

	public function getFields() {
		return $this->_fields;
	}

	/**
	 * @param $post
	 * @return bool 检测是否通过测试
	 */
	function pass($post) {
		if(!is_array($post)) return false;
		if(!$post['brand_name']) return $this->err('请输入品牌');
		if(!$post['make_name']) return $this->err('请输入生产商');
		if(!$post['car_name']) return $this->err('请输入车系');
		if(!$post['model_name']) return $this->err('请输入车款');
		if(!$post['truename']) return $this->err('请输入您的姓名');
		if(!$post['mobile']) return $this->err('请输入手机号');
		if(!$post['address']) return $this->err('请输入地址');
		if(!$post['discount']) return $this->err('请输入已知优惠');
		if(!$post['sale_company']) return $this->err('请输入报价单位');
		return true;
	}


	/**
	 * 获取一条数据
	 * @return array|void
	 */
	function getOne() {
		return  $this->_db->getOne("SELECT * FROM {$this->_table} WHERE itemid=$this->_itemid");
	}

	/**
	 * 获取列表
	 * @param string $condition
	 * @param string $order
	 * @param string $cache
	 * @return array
	 */
	function getList($condition = 'status=3', $order = 'edittime DESC', $cache = '') {
		global $items;
		$num = $this->_db->count($this->_table, $condition, $cache);
		$items=$num;
		$this->_pn->recordsPerPage($this->_pagesize);
		$this->_pn->records($num);
		$sql = "SELECT * FROM {$this->_table}";
		$sql .= $condition ? " WHERE {$condition}" : '';
		$sql .= $order ? " ORDER BY {$order}" : '';
		$sql .= " LIMIT " . ($this->_pn->getPage() - 1) * $this->_pagesize . ',' . $this->_pagesize;
		$lists = $catids = $CATS = array();
		$result = $this->_db->query($sql, $cache);
		while($r = $this->_db->fetchArray($result)) {
			$lists[] = $r;
		}
		return $lists;
	}



	function delete($itemid, $all = true) {
		if(is_array($itemid)) {
			foreach($itemid as $v) {
				$this->delete($v, $all);
			}
		} else {
			if($all) {
				$this->_db->query(Sql::delete($this->_table,"itemid=$itemid"));
			}
		}
	}
}
?>